package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yjc.drygoods.ware.entity.WareInfoEntity;
import com.yjc.drygoods.ware.feign.ProductFeignService;
import com.yjc.drygoods.ware.service.WareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yjc.drygoods.ware.entity.ExchangeDetailEntity;
import com.yjc.drygoods.ware.service.ExchangeDetailService;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 换货需求
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-11-01 16:20:12
 */
@RestController
@RequestMapping("ware/exchangedetail")
public class ExchangeDetailController {
    @Autowired
    private ExchangeDetailService exchangeDetailService;

    @Autowired
    ProductFeignService productFeignService;

    @Autowired
    WareInfoService wareInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = exchangeDetailService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ExchangeDetailEntity exchangeDetail = exchangeDetailService.getById(id);
        R r = productFeignService.getTotalCatelogPathWithSpuId(exchangeDetail.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        List<Integer> data = (List<Integer>) r.get("data");
        List<Long> collect = data.stream().map(item -> {
            return Long.parseLong(item.toString());
        }).collect(Collectors.toList());
        Long[] longs = collect.toArray(new Long[collect.size()]);
        exchangeDetail.setCatelogPathWithSpu(longs);
        return R.ok().put("exchangeDetail", exchangeDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExchangeDetailEntity exchangeDetail) {
        exchangeDetailService.saveExchangeDetail(exchangeDetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExchangeDetailEntity exchangeDetail) {
        WareInfoEntity wareInfo = wareInfoService.getById(exchangeDetail.getWareId());
        exchangeDetail.setWareName(wareInfo.getName());
        exchangeDetailService.updateById(exchangeDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        exchangeDetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
